package com.lzxuni.modules.cropinformation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.cropinformation.entity.TblPlantingInformation;
import com.lzxuni.modules.cropinformation.mapper.TblPlantingInformationMapper;
import com.lzxuni.modules.cropinformation.service.TblPlantingInformationService;
import com.lzxuni.modules.system.service.BaseAreaService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 作物种植信息 服务实现类
 * </p>
 *
 * @author rdf
 * @since 2020-11-26
 */
@Service
public class TblPlantingInformationServiceImpl extends ServiceImpl<TblPlantingInformationMapper, TblPlantingInformation> implements TblPlantingInformationService {

    @Autowired
    private BaseAreaService baseAreaService;

    @Override
    public PageInfo<TblPlantingInformation> queryPage(PageParameter pageParameter, TblPlantingInformation tblPlantingInformation) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    pageParameter.getSidx() + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<TblPlantingInformation> tblPlantingInformationList = baseMapper.queryList(tblPlantingInformation);
        PageInfo<TblPlantingInformation> pageInfo = new PageInfo<>(tblPlantingInformationList);
        return pageInfo;
    }

    //导出
    @Override
    public Workbook exportTemplate(TblPlantingInformation tblPlantingInformation) throws IOException {
        PageHelper.orderBy("create_time" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        List<TblPlantingInformation> tblPlantingList = baseMapper.queryList(tblPlantingInformation);
        List<String> list0 = new ArrayList<>();
        /** 列表页 **/
        list0.add("所属地块");
        list0.add("年份");
        list0.add("作物名称");
        list0.add("作物型号");
        listData1.add(list0);
        if (tblPlantingList != null && tblPlantingList.size() > 0) {
            for(int n = 0; n < tblPlantingList.size(); n++){
                TblPlantingInformation tblPlantingInformation1 = tblPlantingList.get(n);
                List<String> list = new ArrayList<>();
                /** 列表页 **/
                list.add(tblPlantingInformation1.getLandNum());
                list.add(tblPlantingInformation1.getPlantingYear());
                list.add(tblPlantingInformation1.getCropName());
                list.add(tblPlantingInformation1.getCropModel());
//                list.add(MethodUtil.formatDate(0, tblLoan1.getCreateTime()));
                listData1.add(list);
            }
        }
        dataMap.put("作物种植信息", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "作物种植信息");
        return wb;
    }
}
